package com.jerry.netty.buffer;

import java.nio.ByteBuffer;

public class ReadOnlyBufferTest {
    public static void main(String[] args) {
        //创建一个缓冲区
        ByteBuffer buffer = ByteBuffer.allocate(64);

        //向缓冲区中添加数据
        for (int i=0; i<64; i++) {
            buffer.put((byte)i);
        }

        //将Buffer进行读写转换
        buffer.flip();

        //转换为只读buffer
        ByteBuffer readOnlyBuffer = buffer.asReadOnlyBuffer();
        System.out.println(readOnlyBuffer.getClass());

        //从缓冲区中取出数据
        while (readOnlyBuffer.hasRemaining()) {
            System.out.println(readOnlyBuffer.get());
        }

        //ReadOnlyBufferException
        readOnlyBuffer.put((byte)20);

    }
}
